//
//  YTHDBottomView.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/7/16.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTHDBottomView.h"
@interface YTHDBottomView()
@property (weak, nonatomic) IBOutlet UIButton *commentButton;   // 评论按钮
@property (weak, nonatomic) IBOutlet UIButton *collectionButton; // 收藏按钮
@property (weak, nonatomic) IBOutlet UIButton *scollButton; // 评论数量按钮

@end
@implementation YTHDBottomView

// 评论按钮监听
- (IBAction)editButtonClick:(UIButton *)sender
{
    self.begainEditing();
}


#pragma mark: - 分享按钮
- (IBAction)shareButtonClick:(UIButton *)sender
{
    NSAssert(self.shareButtonClick != nil, @"shareButtonClick != nil");
    
    self.shareButtonClick();
}

#pragma mark: - 收藏文章 按钮监听
- (IBAction)collectionButtonClick:(UIButton *)sender
{
    
    if (!YTNetworkManager.shareManager.account.auth) {
        [NSNotificationCenter.defaultCenter postNotificationName:kShouldLoginNotication object:nil];
        return;
    }
    
    [YTNetworkManager.shareManager postFavoriteArticleWithTid:self.model.tid
                                                   isFavorite:sender.selected
                                                   completion:^(BOOL isSucc, NSString *statusInfo)
    {
        if (!isSucc) {
            [self.superview showTipWithTipStr:statusInfo];
            return ;
        }
        
        sender.selected ? [self showTipWithTipStr:@"取消收藏成功"] : (statusInfo ? [self showAwardWithTipStr:[NSString stringWithFormat:@"收藏成功+%@算力",statusInfo]] : [self showTipWithTipStr:@"收藏成功"]);
        
        sender.enabled = YES;
        sender.selected = !sender.selected;
    }];
}
- (void)setArticleIsDisplay:(BOOL)articleIsDisplay
{
    self.scollButton.selected = articleIsDisplay;
}
#pragma mark: - 评论个数按钮 监听
- (IBAction)scrollButtonClick:(UIButton *)sender
{
    
    sender.selected = !sender.selected;
    
    NSAssert(self.scrollButtonClick != nil, @"scrollButtonClick 不能为nil");
    
    self.scrollButtonClick(sender.selected);
}

+ (instancetype)bottomViewWithFrame:(CGRect)frame
{
    UINib *nib = [UINib nibWithNibName:@"YTHDBottomView" bundle:nil];
    YTHDBottomView *view = [nib instantiateWithOwner:nil options:nil][0];
    view.frame = frame;
    return view;
}

- (void)setModel:(YTHomeDetailModel *)model
{
    _model = model;
    
    self.collectionButton.selected = model.isCollect.boolValue;
    [self.scollButton setTitle:model.comment_count forState:UIControlStateNormal];
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.commentButton.layer.masksToBounds = true;
    self.commentButton.layer.cornerRadius = 9;
    self.commentButton.layer.borderWidth = 1;
    self.commentButton.layer.borderColor = [[UIColor colorWithRed:24.0f/255.0f
                                                            green:170.0f/255.0f
                                                             blue:178.0f/255.0f
                                                            alpha:1] CGColor];
    
    //setShadow
    self.layer.masksToBounds = false;
    self.layer.shadowColor = UIColor.lightGrayColor.CGColor;
    self.layer.shadowOffset = CGSizeMake(0, 1);
    self.layer.shadowOpacity = 0.8;
    self.layer.shadowRadius = 5;
}

@end
